{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2646e678",
   "metadata": {},
   "source": [
    "### 切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4007e349",
   "metadata": {},
   "source": [
    "取一个list或tuple的部分元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b0045907",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7903a5cd",
   "metadata": {},
   "source": [
    "L[0:3]表示，从索引0开始，直到索引3为止，但部包括3，即索引0，1，2，正好3个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c04e0e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Sarah', 'Tracy']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6b3157c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Sarah', 'Tracy']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dbc32c09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Bob', 'Jack']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0b91f0fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = list(range(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "280c2c01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6f03f395",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[-10:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f8931853",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[10:20]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5e195af",
   "metadata": {},
   "source": [
    "前10个数，每两个取一个："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3afe8bf9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[:10:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf97c44e",
   "metadata": {},
   "source": [
    "所有数，每隔5个取一个："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b201dd45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L[::5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c50e06e9",
   "metadata": {},
   "source": [
    "tuple也是一种list，唯一区别是tuple不可变。因此，tuple也可以用切片操作，只是操作结果仍然是tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c5d3789d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 1, 2)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(0,1,2,3,4,5)[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d28ba9d",
   "metadata": {},
   "source": [
    "字符串‘XXX’也可以看成是一种list，每个元素就是一个字符，因此字符串也可以用切片操作，操作的结果仍然是字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "037e1799",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ABC'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'ABCDEFG'[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcb4b9a5",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b761053e",
   "metadata": {},
   "source": [
    "有了切片操作，很多地方循环就不再需要了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2946fce",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
